package com.zhonghui.scm.service.impl;

import com.zhonghui.common.utils.DateUtils;
import com.zhonghui.common.utils.SecurityUtils;
import com.zhonghui.scm.domain.ScmPurchasePlan;
import com.zhonghui.scm.domain.ScmPurchasePlanItem;
import com.zhonghui.scm.mapper.ScmPurchaseApplyMapper;
import com.zhonghui.scm.mapper.ScmPurchasePlanMapper;
import com.zhonghui.scm.service.IScmPurchasePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 采购计划Service业务层处理
 *
 * @author zhonghui
 * @date 2022-05-26
 */
@Service
public class ScmPurchasePlanServiceImpl implements IScmPurchasePlanService
{
    @Autowired
    private ScmPurchasePlanMapper scmPurchasePlanMapper;

    /**
     * 查询采购计划
     *
     * @param id 采购计划主键
     * @return 采购计划
     */
    @Override
    public ScmPurchasePlan getById(Long id)
    {
        return scmPurchasePlanMapper.getById(id);
    }

    /**
     * 查询采购计划列表
     *
     * @param scmPurchasePlan 采购计划
     * @return 采购计划
     */
    @Override
    public List<ScmPurchasePlan> getList(ScmPurchasePlan scmPurchasePlan)
    {
        return scmPurchasePlanMapper.getList(scmPurchasePlan);
    }

    /**
     * 新增采购计划
     *
     * @param scmPurchasePlan 采购计划
     * @return 结果
     */
    @Override
    public int add(ScmPurchasePlan scmPurchasePlan)
    {
        scmPurchasePlan.setDocumentNumber(generateNumber("CGJH"));
        scmPurchasePlan.setApplicantId(SecurityUtils.getUserId());
        scmPurchasePlan.setApplicantDate(DateUtils.getNowDate());
        int row = scmPurchasePlanMapper.add(scmPurchasePlan);
        insertItem(scmPurchasePlan);
        return row;
    }

    /**
     * 修改采购计划
     *
     * @param scmPurchasePlan 采购计划
     * @return 结果
     */
    @Override
    public int edit(ScmPurchasePlan scmPurchasePlan)
    {
        scmPurchasePlanMapper.delItem(scmPurchasePlan.getId());
        insertItem(scmPurchasePlan);
        return scmPurchasePlanMapper.edit(scmPurchasePlan);
    }

    /**
     * 批量删除采购计划
     *
     * @param ids 需要删除的采购计划主键
     * @return 结果
     */
    @Override
    public int del(Long[] ids)
    {
        for (Long id : ids) {
            scmPurchasePlanMapper.delItem(id);
        }
        return scmPurchasePlanMapper.del(ids);
    }


    public void insertItem(ScmPurchasePlan scmPurchasePlan){
        List<ScmPurchasePlanItem> scmPurchasePlanItemList = scmPurchasePlan.getScmPurchasePlanItemList();

        if (!scmPurchasePlanItemList.isEmpty()){

        for (ScmPurchasePlanItem scmPurchasePlanItem : scmPurchasePlanItemList) {
            scmPurchasePlanItem.setPurchasingPlanId(scmPurchasePlan.getId());
        }
        scmPurchasePlanMapper.addItem(scmPurchasePlanItemList);

        }
    }

    public String generateNumber(String number){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return number+format.format(date);
    }
}
